﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Microsoft.Samples.Kinect.BodyBasics.Controls
{
    /// <summary>
    /// ServoSlider.xaml 的交互逻辑
    /// </summary>
    public partial class ServoSlider : UserControl
    {
        public ServoSlider()
        {
            servo = new Servo();
            //servo.PWMChangedEvent += Servo_PWMChangedEvent;
            InitializeComponent();
            lbl_servoCount.Content += servo.Number.ToString();

            //设置相对值的改变
            Binding binding_diff_pwm = new Binding();
            binding_diff_pwm.Source = servo.Diff_pwm;
            binding_diff_pwm.Mode = BindingMode.OneWay;
            txb_diffPWM.SetBinding(TextBox.TextProperty, binding_diff_pwm);

            //设置绝对值的改变，只需要改变slider即可
            Binding binding_sldAbs_pwm = new Binding();
            binding_sldAbs_pwm.Source = servo;
            binding_sldAbs_pwm.Path = new PropertyPath("Abs_pwm");
            binding_sldAbs_pwm.Mode = BindingMode.TwoWay;
            sld_PWM.SetBinding(Slider.ValueProperty, binding_sldAbs_pwm);

            //Init_servo();
        }

        private void Servo_PWMChangedEvent(int pwm)
        {
            this.sld_PWM.Value = pwm;
        }

        private Servo servo;

        public Servo Servo { get => servo;}


        public delegate void ServoPWMChangedHandle(Servo servo);
        public event ServoPWMChangedHandle ServoPWMChangedEvent;

        private void Init_servo()
        {
            lbl_servoCount.Content += servo.Number.ToString();
            //txb_absPWM.Text = servo.Abs_pwm.ToString();
            //txb_diffPWM.Text = servo.Diff_pwm.ToString();
            //sld_PWM.Value = servo.Pwm;
            
        }

        //修改值时修改servo内容
        private void sld_PWM_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            txb_absPWM.Text = ((int)sld_PWM.Value).ToString();
            servo.Abs_pwm = (int)sld_PWM.Value;
            //Console.WriteLine("sld_PWM.Value: " + sld_PWM.Value);
            //Console.WriteLine("(int)sld_PWM.Value: " + ((int)sld_PWM.Value).ToString());
            if (ServoPWMChangedEvent != null)
            {
                ServoPWMChangedEvent(servo);
            }

        }

        //private void txb_absPWM_KeyUp(object sender, KeyEventArgs e)
        //{
        //    if (e.Key == Key.Enter)
        //    {
        //        try
        //        {
        //            int pwm = int.Parse(txb_absPWM.Text) + int.Parse(txb_diffPWM.Text);
        //            sld_PWM.Value = pwm;

        //        }
        //        catch(Exception ex)
        //        {
        //            MessageBox.Show(ex.Message);
        //        }
        //    }
        //}
    }
}
